package com.gloaba.logservice.controller.log;

import cn.hutool.core.map.MapUtil;
import com.gloaba.common.core.domain.R;
//import com.gloaba.common.core.web.page.TableDataInfo;
import com.gloaba.logservice.service.log.IApiLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.bson.Document;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 接口请求日志-接口
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/requestLog/")
@Slf4j
public class RequestLogController {

    private final IApiLogService iApiLogService;

    /**
     * 查询
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping("/{date}")
    private R query(@PathVariable("date") String date,
                    @RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                    @RequestParam(value = "pageSize",required = false) Integer pageSize,
                    @RequestBody(required = false)  Map<String,Object> reqMap) {

        if(ObjectUtils.isEmpty(pageIndex)){
            pageSize = MapUtil.getInt(reqMap,"pageSize",100);
            pageIndex = MapUtil.getInt(reqMap,"pageIndex",0);
        }

        if(pageSize>500){
            return R.fail("PageSize cannot exceed 500 at most.");
        }

//        TableDataInfo dataTable = new TableDataInfo();
        Page<Document> pages = iApiLogService.findByPage("RequestLog_"+date, pageIndex, pageSize, "requestTimeStamp", false, new HashMap<>());
//        dataTable.setList(pages.getContent());
//        dataTable.setTotal(pages.getTotalElements());
        return R.ok(pages);
    }

}
